import 'package:flutter/material.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/generated/assets.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/utils/log/utils_log.dart';
import 'package:flutter_09_game/widgets/dialog/common_normal_sheet.dart';
import 'package:flutter_09_game/widgets/image/auto_image.dart';
import 'package:flutter_date_pickers/flutter_date_pickers.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class CustomDatePickerSheet extends StatefulWidget {
  // firstDate: DateTime(2020),
  // lastDate: DateTime(2030),
  const CustomDatePickerSheet({
    super.key,
    this.initialDate,
    this.firstDate,
    this.lastDate,
    this.onDateSelected,
    this.isRangeSelection = false,
    this.initialDateRange,
  });

  final DateTime? initialDate;
  final DateTime? firstDate;
  final DateTime? lastDate;
  final Function(DateTime)? onDateSelected;
  final bool isRangeSelection;
  final DatePeriod? initialDateRange;

  static Future show({
    DateTime? initialDate,
    DateTime? firstDate,
    DateTime? lastDate,
    bool isRangeSelection = false,
    DatePeriod? initialDateRange,
  }) async {
    final result = await CommonNormalSheet(
      title: LocaleKeys.selectTime.tr,
      child: CustomDatePickerSheet(
        initialDate: initialDate,
        firstDate: firstDate,
        lastDate: lastDate,
        isRangeSelection: isRangeSelection,
        initialDateRange: initialDateRange,
      ),
    ).show();

    return result;
  }

  @override
  State<CustomDatePickerSheet> createState() => _CustomDatePickerSheetState();
}

class _CustomDatePickerSheetState extends State<CustomDatePickerSheet> {
  late DateTime selectedDate;
  DatePeriod? selectedDateRange;

  @override
  void initState() {
    super.initState();
    selectedDate = widget.initialDate ?? DateTime.now();
    selectedDateRange = widget.initialDateRange;
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        // _buildHeader(),
        // _buildTitle(),
        _buildDatePicker(),
        _buildActions(),
      ],
    );
  }

  Widget _buildDatePicker() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      child: _buildRangePicker(),
      // widget.isRangeSelection ? _buildRangePicker()
      //       : _buildSinglePicker(),
    );
  }

  // Widget _buildSinglePicker() {
  //   return DayPicker.single(
  //     selectedDate: selectedDate,
  //     onChanged: (DateTime newDate) {
  //       setState(() {
  //         selectedDate = newDate;
  //       });
  //     },
  //     firstDate: widget.firstDate ?? DateTime(2020),
  //     lastDate: widget.lastDate ?? DateTime(2030),
  //     datePickerStyles: DatePickerRangeStyles(
  //       // 日历整体样式
  //       dayHeaderStyle: DayHeaderStyle(
  //         textStyle: TextStyle(
  //           color: AppColor.textPrimary.withOpacity(0.7),
  //           fontSize: 12.sp,
  //           fontWeight: FontWeight.w400,
  //         ),
  //       ),
  //       // 普通日期样式
  //       defaultDateTextStyle: TextStyle(
  //         color: AppColor.textPrimary.withOpacity(0.94),
  //         fontSize: 14.sp,
  //         fontWeight: FontWeight.w400,
  //       ),
  //       // 选中日期样式
  //       selectedDateStyle: TextStyle(
  //         color: AppColor.textPrimary.withOpacity(0.94),
  //         fontSize: 14.sp,
  //         fontWeight: FontWeight.w400,
  //       ),
  //       // 选中日期装饰
  //       selectedSingleDateDecoration: BoxDecoration(
  //         color: AppColor.primary,
  //         shape: BoxShape.circle,
  //       ),
  //       // 当前月份外的日期样式
  //       currentDateStyle: TextStyle(
  //         color: AppColor.textPrimary.withOpacity(0.94),
  //         fontSize: 14.sp,
  //         fontWeight: FontWeight.w400,
  //       ),
  //     ),
  //     datePickerLayoutSettings: DatePickerLayoutSettings(
  //       maxDayPickerRowCount: 6,
  //       showPrevMonthEnd: true,
  //       showNextMonthStart: true,
  //       dayPickerRowHeight: 36.w,
  //       monthPickerPortraitWidth: 343.w,
  //       hideMonthNavigationRow: false,
  //     ),
  //   );
  // }

  Widget _buildRangePicker() {
    return RangePicker(
      selectedPeriod:
          selectedDateRange ?? DatePeriod(DateTime.now(), DateTime.now()),
      onChanged: (DatePeriod newPeriod) {
        setState(() {
          selectedDateRange = newPeriod;
        });
      },
      firstDate: widget.firstDate ?? DateTime(2020),
      lastDate: widget.lastDate ?? DateTime(2030),
      datePickerStyles: DatePickerRangeStyles(
        // 日历整体样式
        dayHeaderStyle: DayHeaderStyle(
          textStyle: TextStyle(
            color: AppColor.textPrimary.withOpacity(0.94),
            fontSize: 12.sp,
            fontWeight: FontWeight.w400,
          ),
        ),
        // 普通日期样式
        defaultDateTextStyle: TextStyle(
          color: AppColor.textPrimary.withOpacity(0.94),
          fontSize: 14.sp,
          fontWeight: FontWeight.w400,
        ),
        // 选中日期样式
        selectedDateStyle: TextStyle(
          color: AppColor.textPrimary.withOpacity(0.94),
          fontSize: 14.sp,
          fontWeight: FontWeight.w400,
        ),
        selectedSingleDateDecoration: const BoxDecoration(
          color: AppColor.primary,
          shape: BoxShape.circle,
        ),
        // 选中范围装饰
        selectedPeriodStartDecoration: const BoxDecoration(
          color: AppColor.primary,
          shape: BoxShape.circle,
        ),
        selectedPeriodLastDecoration: const BoxDecoration(
          color: AppColor.primary,
          shape: BoxShape.circle,
        ),

        selectedPeriodMiddleDecoration: BoxDecoration(
          color: Colors.black.withOpacity(0.3),
          borderRadius: BorderRadius.circular(18.w),
        ),

        // 选中范围中间日期的文本样式
        selectedPeriodMiddleTextStyle: TextStyle(
          color: AppColor.textPrimary.withOpacity(0.94),
          fontSize: 14.sp,
          fontWeight: FontWeight.w400,
        ),
      ),
      datePickerLayoutSettings: DatePickerLayoutSettings(
        maxDayPickerRowCount: 6,
        showPrevMonthEnd: true,
        showNextMonthStart: true,
        dayPickerRowHeight: 36.w,
        monthPickerPortraitWidth: 343.w,
        hideMonthNavigationRow: false,
      ),
    );
  }

  Widget _buildActions() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 16.w)
          .copyWith(bottom: 12.w + ScreenUtil().bottomBarHeight),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          // 取消按钮
          GestureDetector(
            onTap: () => Get.back(),
            child: Text(
              LocaleKeys.cancel.tr,
              style: TextStyle(
                color: AppColor.primary.withOpacity(0.3),
                fontSize: 12.sp,
                fontWeight: FontWeight.w600,
                decoration: TextDecoration.underline,
                decorationColor: AppColor.primary.withOpacity(0.3),
              ),
            ),
          ),
          12.horizontalSpace,

          // 确定按钮
          GestureDetector(
            onTap: () {
              if (widget.isRangeSelection) {
                if (selectedDateRange != null) {
                  Get.back(result: selectedDateRange);
                }
              } else {
                Get.back(result: selectedDate);
              }
            },
            child: Text(
              LocaleKeys.ok.tr,
              style: TextStyle(
                color: AppColor.primary,
                fontSize: 12.sp,
                fontWeight: FontWeight.w600,
                decoration: TextDecoration.underline,
                decorationColor: AppColor.primary,
              ),
            ),
          ),
          16.horizontalSpace,
        ],
      ),
    );
  }
}
